<?
include 'root.php';
Class customer extends Root {
	public function __construct() {
		parent::__construct();
		$this -> load -> helper('util');
	}

	public function index() {
		$this -> view();
	}

	public function view() {
		
		$this -> checkAuthen("customer","view");

		$this -> data['canSearch'] = $this -> canUseFunction("customer","search");
		$this -> data['canCheckIn'] = $this -> canUseFunction("customer","edit");

		$this -> data['header'] = "รายชื่อลูกค้า";
		$this -> pagination -> create_links();
		$this -> load -> model("customermodel");
		$q = $this -> getCoreQuery($this -> data['canSearch']);
		$allRow = $this -> customermodel -> getCustomerCount($this -> checkIDSearch("customer","viewall"), $q);
		$pageConfig = $this -> initPagination($allRow); ;
		$this -> data['pagination'] = $this -> pagination;
		$this -> data['dataList'] = $this -> customermodel -> getCustomer($this -> checkIDSearch("customer","viewall"), $q, $pageConfig['start'], $pageConfig['limit']);
		$this -> includeView("viewcustomer");

	}
	public function ViewOrderOfCustomer($custID) {
		
		$this -> checkAuthen("customer","view");

		//$this -> data['canSearch'] = $this -> canUseFunction("customer","search");
		//$this -> data['canCheckIn'] = $this -> canUseFunction("customer","edit");

		$this -> data['header'] = "ประวัติการสั่งซื้อของลูกค้า";
		$ordermodel = $this->load->model("ordermodel");
		$this -> pagination -> create_links();
		$this -> load -> model("customermodel");
		$allRow = $this -> ordermodel -> getOrderOfCustomerCount($custID);
		$pageConfig = $this -> initPagination($allRow); ;
		$this -> data['pagination'] = $this -> pagination;
		
		$this -> data['dataList'] = $this->ordermodel->getOrderOfCustomer($custID,$pageConfig['start'], $pageConfig['limit']);
		$this -> includeView("vieworderofcustomer");

	}
	public function edit($custID) {
		$this -> checkAuthen("customer","edit");

		$this -> data['header'] = 'Customer Detail';
		$this -> addJS(array("location"));
		$this -> load -> model("customermodel");
		$this -> load -> library('genhtml');
		$this -> data['genhtml'] = $this -> genhtml;
		$this -> data['dataList'] = $this -> customermodel -> getACustomer($custID);

		$this -> genhtml -> setData($this -> data['dataList']);
		$this -> includeView("editcustomer");
	}

	public function updateLocation() {
		$data = $this -> Post2Array($_POST);
		$this -> load -> model("customermodel");
		$this -> customermodel -> updateLoaction($data);
		$this -> set_res("true", "อัพเดท loction เรียบร้อยแล้ว");
	}

	public function updateData() {
		$data = $this -> Post2Array($_POST);
		$this -> load -> model("customermodel");
		$this -> customermodel -> updateCustomer($data);
		$this -> set_res("true", "อัพเดทข้อมูลเรียบร้อยแล้ว");
	}

	public function area() {
		$this -> data['header'] = "พื้นที่แสดงตำแหน่งของลูกค้าทั้งหมด";
		$this -> load -> model("customermodel");
		$this -> data['dataList'] = $this -> customermodel -> getCustomerArea($this -> checkIDSearch("customer","viewall"), "");
		
		$this -> includeView("areamap");
	}

}
?>